package com.cqcetbbs.smartlab.cqcetbbs.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by user on 2017/9/26.
 *
 * fragment的父类
 *
 */

public abstract class BaseFragment  extends Fragment {

//    boolean isViewCreated = false;
//    boolean isVisible = false;

    //作为Context使用
    public  Activity activity;
    private View mRootView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = getActivity();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
//        isViewCreated = true;
    }

    //fragmeng创建时加载布局
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = getInitView();
        return mRootView;
    }


    //Activity创建时加载数据
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
       initData();
//        lazyload();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }


    //fragment可见或不可见是调用
//    @Override
//    public void setUserVisibleHint(boolean isVisibleToUser) {
//        super.setUserVisibleHint(isVisibleToUser);
//        if(isVisibleToUser){
//            isVisible = true;
//            lazyload();
//        }
//    }


//    public void lazyload(){
//        if(isViewCreated&&isVisible){
//            isViewCreated = false;
//            isVisible = false;
//            initData();
//        }
//    }

    // 子类必须实现的方法
    public abstract View getInitView();

    public void initData() {

    }
}
